package com.bw.aop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.domain.SysUser;
import com.bw.domain.Tlog;
import com.bw.service.SysUserService;
import com.bw.service.TlogService;
import com.bw.utils.common.JWTUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

//@Component
//@Aspect
public class Aop {
    @Autowired
    TlogService tlogService;
    @Autowired
    SysUserService sysUserService;
    @Resource
    HttpServletRequest request;
    @Around(value = "execution(* com.bw.controller.*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        String userId = JWTUtils.getUsername();
        String username="未知用户";
        if(userId!=null){
            SysUser one = sysUserService.getById(userId);
             username = one.getUsername();
        }
        String name = joinPoint.getSignature().getName();
        String ip = request.getRemoteAddr();
        String url = request.getRequestURI();
        Date reqestime = new Date();
        Tlog tlog = new Tlog(null,username,name,ip,url,reqestime);
        tlogService.save(tlog);
        return  joinPoint.proceed();
    }
}
